//
//  ContactDetailVC.swift
//  com.yunshu.yunshuapp
//
//  Created by 周文杰 on 2017/6/14.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit
import Kingfisher

class ContactDetailVC: UIViewController {

    var model : Contact?
    
    let contentView = UIScrollView.init(frame: CGRect.init(x: 0, y: 0, width: Const.SCREEN_WIDTH, height: Const.SCREEN_HEIGHT))

    let logoView = UIImageView.init(frame: CGRect.init(x: 20, y: 20, width: Const.SCREEN_WIDTH-40, height: Const.SCREEN_WIDTH-40))
    
    let nameLabel = UILabel.init(frame: CGRect.init(x: 20, y: Const.SCREEN_WIDTH - 10, width: Const.SCREEN_WIDTH-20, height: 30))
    
    let grayView = UIView.init(frame: CGRect.init(x: 10, y: Const.SCREEN_WIDTH + 20, width: Const.SCREEN_WIDTH - 20, height: 50))
    
    let desLabel = UILabel.init(frame: CGRect.init(x: 10, y: 10, width: Const.SCREEN_WIDTH - 40, height: 30))
    
    let button = UIButton.init(type: UIButton.ButtonType.roundedRect)
    
    init() {
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.contentView.backgroundColor = self.view.backgroundColor
        self.view.addSubview(self.contentView)
        self.title = "厂家详情"
        self.logoView.tag = 99
        self.logoView.kf.setImage(with: URL.init(string: (model?.logo)!))
        self.contentView.addSubview(self.logoView)
        
        self.contentView.addSubview(self.spacing())

        self.nameLabel.font = UIFont.systemFont(ofSize: 17)
        self.nameLabel.text = model?.name
        self.nameLabel.numberOfLines = 0
        _ = self.adjustHeight(self.nameLabel)
        self.nameLabel.tag = 99
        self.contentView.addSubview(self.nameLabel)
        
        self.contentView.addSubview(self.spacing())

        self.desLabel.font = UIFont.systemFont(ofSize: 13)
        self.desLabel.textColor = Const.WTM_GRAY
        self.desLabel.numberOfLines = 0
        self.desLabel.text = model?.briefing
        let desHeight = self.adjustHeight(self.desLabel)
        self.grayView.addSubview(self.desLabel)
        
        self.button.frame = CGRect.init(x: Const.SCREEN_WIDTH - 120, y: desHeight + 20, width: 100, height: 40)
        self.button.setTitleColor(Const.WTM_GREEN, for: .normal)
        self.button.setTitle("官方网站", for: .normal)
        self.button.addTarget(self, action: #selector(ContactDetailVC.enterWebsite), for: .touchUpInside)
        self.grayView.addSubview(self.button)
        self.grayView.backgroundColor = Const.WTM_background_gray
        self.grayView.layer.cornerRadius = 5
        self.grayView.tag = 99
        self.setHeight( desHeight + 60, onView : self.grayView )
        self.contentView.addSubview(self.grayView)
        
        self.contentView.addSubview(self.spacing())

        self.addItem("产品列表").addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(ContactDetailVC.enterProducts)))
        self.addItem("联系方式").addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(ContactDetailVC.enterPrice)))

    }

    func spacing() ->UIView {
        let spacing = UIView.init(frame: CGRect.init(x: 0, y: 0, width: Const.SCREEN_WIDTH, height: 10))
        spacing.tag = 99
        return spacing
    }
    
    func addItem(_ name : String ) ->UIView{
        let container = UIView.init(frame: CGRect.init(x: 0, y: 0, width: Const.SCREEN_WIDTH, height: 52))
        container.tag = 99
        let titleLabel = UILabel.init(frame: CGRect.init(x: 15, y: 0, width: 100, height: 52))
        titleLabel.text = name
        container.addSubview(titleLabel)
        
        let line = UIView.init(frame: CGRect.init(x: 10, y: 52 - Const.ONE_PIXEL, width: Const.SCREEN_WIDTH - 20, height: Const.ONE_PIXEL))
        line.backgroundColor = Const.WTM_LIGHTGRAY
        container.addSubview(line)

        let arrow = UIImageView.init(frame: CGRect.init(x: Const.SCREEN_WIDTH - 40 , y: 11, width: 30, height: 30))
        arrow.image = UIImage.init(named: "right_arrow")
        container.addSubview(arrow)
        
        self.contentView.addSubview(container)
        return container
    }

    func setHeight(_ desHeight:CGFloat, onView : UIView ){
        var frame = onView.frame
        frame.size.height = desHeight
        onView.frame = frame
    }
    
    func adjustHeight(_ lable : UILabel ) -> CGFloat {
        let width = lable.frame.size.width
        let titleHeight = UIUtility.calcStringHeight(lable.text!, font: lable.font, refWidth: width)
        self.setHeight(titleHeight + 2, onView:lable)
        
        return titleHeight + 2
    }
    
    @objc func enterProducts() {
        let vc:WebViewController = UIStoryboard.init(name: "mainStoryBoard", bundle: Bundle.main).instantiateViewController(withIdentifier: WebViewController.VCID) as! WebViewController
        var url = NetworkManager.BASE_URL + NetworkManager.TONGXUNLU_DETAIL_PRODUCT
        url.append("&id=" + (model?.contactId)!)
        url.append("&token=" + User.getToken()!)
        vc.url = url
        vc.title = "产品列表"
        self.navigationController?.pushViewController(vc, animated: true)

    }

    @objc func enterWebsite() {
        guard let url = self.model?.officialWebsite  else {
            self.navigationController?.view.makeToast("对不起，该厂家暂不提供官网链接")
            return
        }
        let vc:WebViewController = UIStoryboard.init(name: "mainStoryBoard", bundle: Bundle.main).instantiateViewController(withIdentifier: WebViewController.VCID) as! WebViewController
        vc.url = url
        self.navigationController?.pushViewController(vc, animated: true)
        

    }
    
    func dial() {
        let str="telprompt://" + (model?.tel)!
        // NSLog(@"str======%@",str);
        UIApplication.shared.openURL(URL.init(string: str)!)
    }

    @objc func enterPrice() {
        let vc:WebViewController = UIStoryboard.init(name: "mainStoryBoard", bundle: Bundle.main).instantiateViewController(withIdentifier: WebViewController.VCID) as! WebViewController
        var url = NetworkManager.BASE_URL + NetworkManager.TONGXUNLU_DETAIL_PRICE
        url.append("&id=" + (model?.contactId)!)
        url.append("&token=" + User.getToken()!)
        vc.url = url
        vc.title = "价格政策"
        self.navigationController?.pushViewController(vc, animated: true)
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        var y:CGFloat = 20
        for iView in contentView.subviews {
            if iView.tag == 99 {
                var frame = iView.frame
                frame.origin.y = y
                y += frame.size.height
                iView.frame = frame
            }
        }
        
        self.contentView.contentSize = CGSize.init(width: Const.SCREEN_WIDTH, height: y + 20)
    }

}
